// 行为执行状态枚举
export enum BehaviorExecutionStatus {
  PENDING = 'PENDING',           // 待执行
  RUNNING = 'RUNNING',           // 执行中
  SUCCESS = 'SUCCESS',           // 执行成功
  FAILED = 'FAILED',             // 执行失败
  COMPENSATING = 'COMPENSATING', // 补偿中
  COMPENSATED = 'COMPENSATED',   // 已补偿
  CANCELLED = 'CANCELLED',       // 已取消
  TIMEOUT = 'TIMEOUT',           // 超时
}

// 行为类型枚举
export enum BehaviorType {
  CREATE = 'CREATE',             // 创建
  UPDATE = 'UPDATE',             // 更新
  DELETE = 'DELETE',             // 删除
  START = 'START',               // 启动
  STOP = 'STOP',                 // 停止
  RESTART = 'RESTART',           // 重启
  SCALE = 'SCALE',               // 扩缩容
  MIGRATE = 'MIGRATE',           // 迁移
  BACKUP = 'BACKUP',             // 备份
  RESTORE = 'RESTORE',           // 恢复
  DEPLOY = 'DEPLOY',             // 部署
  ROLLBACK = 'ROLLBACK',         // 回滚
  CONFIGURE = 'CONFIGURE',       // 配置
  MONITOR = 'MONITOR',           // 监控
  NOTIFY = 'NOTIFY',             // 通知
  CUSTOM = 'CUSTOM',             // 自定义
}

// 行为优先级枚举
export enum BehaviorPriority {
  LOW = 'LOW',                   // 低优先级
  NORMAL = 'NORMAL',             // 普通优先级
  HIGH = 'HIGH',                 // 高优先级
  URGENT = 'URGENT',             // 紧急优先级
  CRITICAL = 'CRITICAL',         // 关键优先级
}

// 补偿策略枚举
export enum CompensationStrategy {
  NONE = 'NONE',                 // 无补偿
  ROLLBACK = 'ROLLBACK',         // 回滚
  RETRY = 'RETRY',               // 重试
  ALTERNATIVE = 'ALTERNATIVE',   // 替代方案
  MANUAL = 'MANUAL',             // 手动处理
  AUTO_FIX = 'AUTO_FIX',         // 自动修复
}

// 行为定义接口
export interface BehaviorDefinition {
  id: string;
  name: string;
  code: string;
  description?: string;
  type: BehaviorType;
  priority: BehaviorPriority;
  resourceType: string;
  parameters: BehaviorParameter[];
  conditions: BehaviorCondition[];
  actions: BehaviorAction[];
  compensationStrategy: CompensationStrategy;
  timeout: number; // 超时时间（秒）
  retryCount: number; // 重试次数
  retryInterval: number; // 重试间隔（秒）
  enabled: boolean;
  createdAt: string;
  updatedAt: string;
}

// 行为参数接口
export interface BehaviorParameter {
  name: string;
  type: 'string' | 'number' | 'boolean' | 'object' | 'array';
  required: boolean;
  defaultValue?: any;
  description?: string;
  validation?: {
    min?: number;
    max?: number;
    pattern?: string;
    enum?: string[];
  };
}

// 行为条件接口
export interface BehaviorCondition {
  id: string;
  field: string;
  operator: 'eq' | 'ne' | 'gt' | 'gte' | 'lt' | 'lte' | 'in' | 'nin' | 'contains' | 'regex';
  value: any;
  logicalOperator?: 'and' | 'or';
}

// 行为动作接口
export interface BehaviorAction {
  id: string;
  name: string;
  type: 'api' | 'function' | 'workflow' | 'notification';
  target: string;
  parameters: Record<string, any>;
  order: number;
  enabled: boolean;
}

// 行为执行实例接口
export interface BehaviorExecution {
  id: string;
  behaviorId: string;
  behaviorName: string;
  resourceId: string;
  resourceType: string;
  status: BehaviorExecutionStatus;
  priority: BehaviorPriority;
  parameters: Record<string, any>;
  result?: any;
  error?: string;
  startTime: string;
  endTime?: string;
  duration?: number;
  retryCount: number;
  maxRetryCount: number;
  compensationExecutions: CompensationExecution[];
  logs: BehaviorLog[];
  metadata: Record<string, any>;
}

// 补偿执行接口
export interface CompensationExecution {
  id: string;
  behaviorExecutionId: string;
  strategy: CompensationStrategy;
  status: 'PENDING' | 'RUNNING' | 'SUCCESS' | 'FAILED';
  parameters: Record<string, any>;
  result?: any;
  error?: string;
  startTime: string;
  endTime?: string;
  duration?: number;
}

// 行为日志接口
export interface BehaviorLog {
  id: string;
  behaviorExecutionId: string;
  level: 'DEBUG' | 'INFO' | 'WARN' | 'ERROR';
  message: string;
  timestamp: string;
  metadata?: Record<string, any>;
}

// 行为编排接口
export interface BehaviorOrchestration {
  id: string;
  name: string;
  description?: string;
  resourceType: string;
  behaviors: OrchestrationBehavior[];
  conditions: OrchestrationCondition[];
  enabled: boolean;
  createdAt: string;
  updatedAt: string;
}

// 编排行为接口
export interface OrchestrationBehavior {
  behaviorId: string;
  behaviorName: string;
  order: number;
  parallel: boolean;
  dependsOn?: string[];
  timeout: number;
  retryCount: number;
  compensationBehaviorId?: string;
}

// 编排条件接口
export interface OrchestrationCondition {
  id: string;
  type: 'pre' | 'post';
  behaviorId?: string;
  condition: BehaviorCondition;
  action: 'continue' | 'skip' | 'stop' | 'compensate';
}

// 行为统计接口
export interface BehaviorStatistics {
  totalExecutions: number;
  successCount: number;
  failureCount: number;
  pendingCount: number;
  runningCount: number;
  averageDuration: number;
  successRate: number;
  failureRate: number;
  topBehaviors: Array<{
    behaviorId: string;
    behaviorName: string;
    executionCount: number;
    successRate: number;
  }>;
}

// 行为监控接口
export interface BehaviorMonitor {
  id: string;
  behaviorId: string;
  resourceId: string;
  metrics: {
    executionCount: number;
    successCount: number;
    failureCount: number;
    averageDuration: number;
    lastExecutionTime?: string;
    nextExecutionTime?: string;
  };
  alerts: BehaviorAlert[];
  status: 'normal' | 'warning' | 'critical';
}

// 行为告警接口
export interface BehaviorAlert {
  id: string;
  behaviorId: string;
  resourceId: string;
  type: 'execution_failure' | 'timeout' | 'compensation_failure' | 'custom';
  severity: 'low' | 'medium' | 'high' | 'critical';
  message: string;
  timestamp: string;
  resolved: boolean;
  resolvedAt?: string;
  resolvedBy?: string;
}
